<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?><?php defined( '_SEXEC' ) or die( 'Restricted access' ); ?>
<?php
	if(!isset($_SESSION['username']) && !isset($_SESSION['usertype']))
		header("location: index.php");
?>
<?php

if(isset($submit))
{
	switch(trim($submit))
	{
		case "Save":
				b($companyname,"Enter Company Name");
				b($companycode,"Enter Unique Company Code");
				i($surcharge,"Enter Surcharge in Number");
				i($insurance_charge,"Enter Insurance Charge in Number");
				if($companycode != ""){
					$sql = "select count(*) as `count` from companies where companycode = \"$companycode\"";
					$result = mysql_query($sql);
					$row = mysql_fetch_array($result);
					if($row['count'] != 0){
						$arr[] = "Company Code already exists for another Company";
					}
				}
				if(count($arr) == 0) {
					$sql = "insert into companies(companyname,companycode,contactperson,surcharge,address,city,state,pincode,email,mobile,phone,fax,road_charge,insurance_charge,service_tax,status,datetime) values(\"$companyname\",\"$companycode\",\"$contactperson\",\"$surcharge\",\"$address\",\"$city\",\"$state\",\"$pincode\",\"$email\",\"$mobile\",\"$phone\",\"$fax\",\"$road_charge\",\"$insurance_charge\",\"$service_tax\",\"$status\",now())";
					mysql_query($sql);
					$msg = "Company added succesful";
					$companyname=$companycode=$contactperson=$surcharge=$address=$city=$state=$pincode=$email=$mobile=$phone=$fax=$road_charge=$insurance_charge=$service_tax=$status="";
				}
				break;
		case "Update":
				if($step == 2) {
					b($companyname,"Enter Company Name");
					b($companycode,"Enter Unique Company Code");
					i($surcharge,"Enter Surcharge in Number");
					i($insurance_charge,"Enter Insurance Charge in Number");
					if($companycode != ""){
						$sql = "select count(*) as `count` from companies where companycode = \"$companycode\" and id != '$id'";
						$result = mysql_query($sql);
						$row = mysql_fetch_array($result);
						if($row['count'] != 0){
							$arr[] = "Company Code already exists for another Company";
						}
					}
					if(count($arr) == 0) {
						$sql = "update companies set companyname=\"$companyname\", companycode=\"$companycode\", contactperson=\"$contactperson\", surcharge=\"$surcharge\", address=\"$address\", city=\"$city\", state=\"$state\", pincode=\"$pincode\", email=\"$email\", mobile=\"$mobile\", phone=\"$phone\", fax=\"$fax\", road_charge=\"$road_charge\", insurance_charge=\"$insurance_charge\", service_tax=\"$service_tax\", status=\"$status\", datetime=now() where id = '$id'";
						mysql_query($sql);
						$msg = "Company updated succesful";
						$companyname=$companycode=$contactperson=$surcharge=$address=$city=$state=$pincode=$email=$mobile=$phone=$fax=$road_charge=$insurance_charge=$service_tax=$status="";
					} else {
				  		$btval="     Update     ";
				 	}
				} 
				else if(isset($eid)) {
					$sql = "select * from companies where id = '$eid'";
					$result = mysql_query($sql);
					$row = mysql_fetch_array($result);
					$id = $row['id'];
					$companyname = $row['companyname'];
					$companycode = $row['companycode'];
					$contactperson = $row['contactperson'];
					$surcharge = $row['surcharge'];
					$address = $row['address'];
					$city = $row['city'];
					$state = $row['state'];
					$pincode = $row['pincode'];
					$email = $row['email'];
					$mobile = $row['mobile'];
					$phone = $row['phone'];
					$fax = $row['fax'];
					$road_charge = $row['road_charge'];
					$insurance_charge = $row['insurance_charge'];
					$service_tax = $row['service_tax'];
					$status = $row['status'];
					$step=2;
					$btval="     Update     ";
				}
				break;
		case "Delete":
				$sql = "delete from companies where id = '$eid'";
				mysql_query($sql);
				$msg = "Company delete succesful";
				break;
	}
}

if(!isset($btval))
	$btval="     Save     ";
	


?>

<div id="title">Company Management</div>

<form name="company-form" method="post" action="">
<table border="0" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td colspan=2>
			<?php
				se();
			?>
		</td>
	</tr>
	<tr>
		<td>
			Company Name <br />
			<input name="companyname" type="text" value="<?php echo $companyname?>"/>
		</td>
		<td>
			Company Code <br />
			<input name="companycode" type="text" value="<?php echo $companycode?>"/>
		</td>
	</tr>
	<tr>
		<td>
			Contact Person <br />
			<input name="contactperson" type="text" value="<?php echo $contactperson?>"/>
		</td>
		<td>
			Fuel Surcharges (%) <br />
			<input name="surcharge" type="text" value="<?php echo $surcharge?>"/>
		</td>
	</tr>
	<tr>
		<td>
			Address <br />
			<input name="address" type="text" value="<?php echo $address?>"/>
		</td>
		<td>
			City <br />
			<input name="city" type="text" value="<?php echo $city?>"/>
		</td>
	</tr>
	<tr>
		<td>
			State <br />
			<input name="state" type="text" value="<?php echo $state?>"/>
		</td>
		<td>
			Pincode <br />
			<input name="pincode" type="text" value="<?php echo $pincode?>"/>
		</td>
	</tr>
	<tr>
		<td>
			email <br />
			<input name="email" type="text" value="<?php echo $email?>"/>
		</td>
		<td>
			Mobile <br />
			<input name="mobile" type="text" value="<?php echo $mobile?>"/>
		</td>
	</tr>
	<tr>
		<td>
			Phone <br />
			<input name="phone" type="text" value="<?php echo $phone?>"/>
		</td>
		<td>
			Fax <br />
			<input name="fax" type="text" value="<?php echo $fax?>"/>
		</td>
	</tr>
	<tr>
		<td>
			By Road Charge <br />
			<input name="road_charge" type="text" value="<?php echo $road_charge?>"/>
		</td>
		<td>
			Insurance Charge (%)<br />
			<input name="insurance_charge" type="text" value="<?php echo $insurance_charge?>"/>
		</td>
	</tr>
	<tr>
		<td colspan=2>
			Apply Service Tax:
			<?php
				if($service_tax == "")
					$service_tax = 1;
				foreach($yesno_arr as $k => $v) {
					$st = ($service_tax == $k) ? 'checked' : '';
					echo "&nbsp;&nbsp;<input type='radio' name='service_tax' value='$k' $st> ".$v;
				}
			
			?>
		</td>
	</tr>
	<tr>
		<td colspan=2>
			Status:
			<?php
				if($status == "")
					$status = 1;
				foreach($status_arr as $k => $v) {
					$st = ($status == $k) ? 'checked' : '';
					echo "&nbsp;&nbsp;<input type='radio' name='status' value='$k' $st> ".$v;
				}
			
			?>
		</td>
	</tr>
	<tr>
		<td colspan=2>&nbsp;</td>
	</tr>
	<tr>
		<td colspan="2" align="center">
			<input type=hidden name=id value="<?php echo $id?>" />
			<input type=hidden name=step value="<?php echo $step?>" />
			<input type="submit" name="submit" value="<?php echo $btval?>" class='button'/>
		</td>
	</tr>	
</table>
</form>

<?php
echo "<hr />";
$table = "companies";
$cols = "id, companyname as `Company`, surcharge as `Surcharge (%)`, road_charge, insurance_charge as `insurance_charge (%)`, address, status";
$orderby = ($orderby == '') ? "companyname" : $orderby;
$sort = ($sort == '') ? "asc" : $sort;
$pagelength = 50;
$reparr = array();
foreach($status_arr as $k => $v){
	$reparr['status'][$k] = $v;	
}
gettable("$table",$cols,"$where","$orderby","$sort",$pg,"<a href='?eid=\$id&submit=Update' style='text-decoration:none'>Update</a>&nbsp;|&nbsp;<a href=\"?eid=\$id&submit=Delete\" onclick=\"javascript:return confirm('Are you sure you want to Delete?')\" style='text-decoration:none'>Delete</a>",Array("id"),$reparr);
?>